package com.mew.chess.core.game.pokdeng.game.core;

import cn.hutool.core.util.RandomUtil;
import com.mew.chess.core.common.handler.ChessPushMessageUtil;
import com.mew.chess.core.game.pokdeng.game.PokDengGameManager;
import com.mew.chess.core.game.pokdeng.game.PokDengSeatManager;
import com.mew.chess.core.game.pokdeng.game.PokDengTableManager;
import com.mew.chess.core.game.pokdeng.game.common.PokDengConstantValue;
import com.mew.chess.core.game.pokdeng.game.common.PokDengPushCMDMapType;
import com.mew.chess.core.game.pokdeng.game.message.PushBetMessage;
import com.mew.chess.core.game.pokdeng.game.message.UserMessage;
import com.mew.common.config.NicknameUtil;
import com.mew.common.enm.UserType;
import com.mew.common.util.DateUtils;
import com.mew.common.util.NumberUtil;
import com.mew.common.util.RandomIdUtil;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author dylin
 * @create 2021/5/19
 */
public class RobotUtil {

    public static UserMessage builderRobot(long min){
        UserMessage userMessage = new UserMessage();
        userMessage.buildRobotUserMessage((int) min,PokDengConstantValue.ROBOT_GOLD_SEC);
        userMessage.setPlayTimes(NumberUtil.randomIntByStr(PokDengConstantValue.ROBOT_MAX_PLAY_NUMBER, "-"));
        userMessage.setKickGold(NumberUtil.randomIntByStr(PokDengConstantValue.ROBOT_KICK_NUMBER, "-"));
        return userMessage;
    }

    /**
     * 机器人压注
     */
    public static void robotBet(int roomId,int tableId){
        PokDengTableManager table= PokDengGameManager.getInstance().getController(roomId).getTableManager(tableId);
        List<PokDengSeatManager> seats = table.getSeatMap().values().stream().filter(seat -> null != seat.getUser() && seat.getUser().getUserType() == UserType.Robot.code()).collect(Collectors.toList());
        Collections.shuffle(seats);
        PokDengSeatManager seat;
        int s=6;
        try {
            while(seats.size()>0&&s>0){
                int cLength=RandomUtil.randomInt(seats.size());
                TimeUnit.MILLISECONDS.sleep(500);
                for (int i = 0; i < cLength; i++) {
                    seat=seats.get(0);
                    seats.remove(0);
                    if(null==seat.getUser()||seat.getUser().getUserType()!=UserType.Robot.code()||seat.getUser().getBet()>0){
                        continue;
                    }
                    UserMessage robot=seat.getUser();
                    robot.setBet(table.getBetArrays()[RandomUtil.randomInt(table.getBetArrays().length)]);
                    PushBetMessage message = table.getPushBetMessage();
                    message.setGid(robot.getGid());
                    message.setSeatNo(seat.getSeatNo());
                    message.setBet(robot.getBet());
                    ChessPushMessageUtil.broadcastMessage(table.getOnlineUser().values(), PokDengPushCMDMapType.BetInfoMessage.code(),message);
                    table.skipBetLink();
                }
                s--;
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        for (int i = 0; i < seats.size(); i++) {
            seat=seats.get(0);
            seats.remove(0);
            if(null==seat.getUser()||seat.getUser().getUserType()!=UserType.Robot.code()||seat.getUser().getBet()>0){
                continue;
            }
            UserMessage robot=seat.getUser();
            robot.setBet(table.getBetArrays()[RandomUtil.randomInt(table.getBetArrays().length)]);
            PushBetMessage message = table.getPushBetMessage();
            message.setGid(robot.getGid());
            message.setSeatNo(seat.getSeatNo());
            message.setBet(robot.getBet());
            ChessPushMessageUtil.broadcastMessage(table.getOnlineUser().values(), PokDengPushCMDMapType.BetInfoMessage.code(),message);
            table.skipBetLink();
        }
    }
}
